package team.yidong.fenda.controller;

import org.apache.http.protocol.HTTP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import team.yidong.fenda.service.HospitalProfileService;
import team.yidong.fenda.utils.ImageCheck;
import team.yidong.fenda.utils.QiNiuYunUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by xuanlong on 2018/3/21.
 */
@Controller
public class HospitalProfileController {
    @Autowired
    HospitalProfileService hospitalProfileService;
    @Autowired
    ImageCheck imageCheck;
    @Autowired
    QiNiuYunUtil qiNiuYunUtil;

    @RequestMapping("/hospitalProfile")
    public String hospitalProfile(Model model, HttpSession session) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        model.addAttribute("hospitalProfiles", hospitalProfileService.findHospitalProfile(hospitalId));
        return "hospitalProfile";
    }

    @RequestMapping("/addHospitalProfile")
    @ResponseBody
    public String addHospitalProfile(@RequestParam String headline, HttpSession session, @RequestParam String content, HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("profileImg");
        MultipartFile multipartFile = null;
        String fileName = new String();
        List<String> imgPathList = new ArrayList<>();
        for (int i = 0; i < files.size(); ++i) {
            multipartFile = files.get(i);
            if (!multipartFile.isEmpty()) {
                try {
                    if (imageCheck.isImage(multipartFile.getBytes())) {
                        // 获取文件的后缀名
                        fileName = multipartFile.getOriginalFilename();
                        String suffixName = fileName.substring(fileName.lastIndexOf("."));
                        //uuid为文件命名
                        fileName = UUID.randomUUID() + suffixName;
                        qiNiuYunUtil.uploadFiles(multipartFile.getBytes(), fileName);
                        imgPathList.add("https://qny.liuxuanlong.xyz/" + fileName);
                    } else {
                        return "第" + (i + 1) + "张图片不是图片文件";
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return "第" + (i + 1) + "张图片上传失败";
                }
            }
        }

        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");

        hospitalProfileService.addHospitalProfile(hospitalId, content, headline, imgPathList);
        return "添加成功";

    }

    @RequestMapping("/deleteHospitalProfile")
    @ResponseBody
    public void deleteHospitalProfile(HttpSession session) {

    }

    @RequestMapping("/updateHospitalProfile")
    @ResponseBody
    public String updateHospitalProfile(String headline, HttpSession session, String content, HttpServletRequest request) {
        System.out.println(headline+"*******"+content);
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("profileImg");
        MultipartFile multipartFile = null;
        String fileName = new String();
        List<String> imgPathList = new ArrayList<>();
        for (int i = 0; i < files.size(); ++i) {
            multipartFile = files.get(i);
            if (!multipartFile.isEmpty()) {
                try {
                    if (imageCheck.isImage(multipartFile.getBytes())) {
                        // 获取文件的后缀名
                        fileName = multipartFile.getOriginalFilename();
                        String suffixName = fileName.substring(fileName.lastIndexOf("."));
                        //uuid为文件命名
                        fileName = UUID.randomUUID() + suffixName;
                        qiNiuYunUtil.uploadFiles(multipartFile.getBytes(), fileName);
                        imgPathList.add("https://qny.liuxuanlong.xyz/" + fileName);
                    } else {
                        return "第" + (i + 1) + "张图片不是图片文件";
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return "第" + (i + 1) + "张图片上传失败";
                }
            }
        }
        hospitalProfileService.updateHospitalProfile(hospitalId,content, headline, imgPathList);
        return "添加成功";

    }
}
